﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebGioiThieu.Models;

namespace WebGioiThieu.Controllers
{
    public class MenuItemController : Controller
    {
        private IntroductionWebDBEntities db = new IntroductionWebDBEntities();

        //
        // GET: /MenuItem/

        public ViewResult Index()
        {
            var menuitems = db.MenuItems.Include(m => m.MenuType);
            return View(menuitems.ToList());
        }

        public ViewResult ViewMenuItemByMenuType(int id)
        {
            return View(db.MenuItems.Where(i => i.MenuTypeID == id).ToList());
        }

        [ChildActionOnly]
        public ActionResult ViewMainMenu()
        {
            return PartialView(db.MenuItems.Where(i => i.MenuTypeID == 1).ToList());
        }

        [ChildActionOnly]
        public PartialViewResult GetMainCategories()
        {
            var model = db.MenuItems.ToList()
                .Where(i=>i.TrangThai== true)
                .Where(i => i.MenuTypeID == 1 && i.Parent == 0)
                .Select(x => new MenuModel
                {
                    Url = x.NiceUrl,
                    TieuDe = x.Ten,
                    DisplayOrder = x.ThuTu,
                    ID = x.ID
                }).ToList();

            return PartialView(model);
        }

        [ChildActionOnly]
        public PartialViewResult GetChildCategories(int parentId)
        {
            
            return PartialView(db.MenuItems.ToList().Where(i => i.TrangThai == true).Where(i => i.MenuTypeID == 1 && i.Parent == parentId)
                .Select(x => new MenuModel
                {
                    Url = x.NiceUrl,
                    TieuDe = x.Ten,
                    DisplayOrder = x.ThuTu,
                    ID = x.ID
                }).ToList());
        }

        public static string CreateUrl()
        {
            var url = string.Empty;
            //if (!string.IsNullOrEmpty(menu.BiDanh))
            //{
            //    url = menu.BiDanh;
            //}

            //url = UrlConfigs.TourUrl.Replace("{Bidanh}", menu.BiDanh);

            return url;
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}